package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLHouseDBManager;
import com.qiantu.api.entity.ControlDataAirConditionerBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.TimeLimitBean;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.dialog.BottomTimeLimitDialog;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.utils.Constants;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.ui.activity.map.LocationActivity;
import com.qiantu.phone.ui.dialog.BottomEditDialog;
import com.lxj.xpopup.XPopup;
import com.qiantu.phone.utils.SPUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HouseEditActivity extends AppActivity {
    private HouseBean editHouseInfo;
    private SettingBar mName;
    private SettingBar mLocation;
    private SettingBar mRoomShow;
    private BottomEditDialog mBottomEditDialog;
    private BottomListDialog mBottomListDialog;
    private Map<String,String> firstShowMap;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_house_edit;
    }

    @Override
    protected void initView() {
        mName = findViewById(R.id.name);
        mLocation = findViewById(R.id.location);
        mRoomShow = findViewById(R.id.room_first_show);
        setOnClickListener(mName, mLocation,mRoomShow);
    }

    @Override
    protected void initData() {
        String firstShow =  SPUtils.getInstance().getString ("firstShow");
        if(firstShow.equals("")){
            firstShowMap = new HashMap<>();
        }else{
            firstShowMap = JSON.parseObject(firstShow,new TypeReference<HashMap<String, String>>() {});
        }
        editHouseInfo = AppApplication.getInstance().getCurHouseInfo();
        mName.setRightText(editHouseInfo.getName());
        mLocation.setRightText(editHouseInfo.getAddress());
        mRoomShow.setRightText(getFirstShowData());
    }

    @Override
    public void onClick(View view) {
        if (view == mName){
            showBottomEditDialog(mName.getRightText().toString());
        }else if (view == mLocation){
            LocationData locationData = new LocationData();
            locationData.location = editHouseInfo.getLongitude()+":"+ editHouseInfo.getLatitude();
            LocationActivity.startForResult(this, locationData, LocationActivity.LOCATION_REQUEST_CODE);
        }else if(view == mRoomShow){
            Intent intent = new Intent(this, RoomShowSettingActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onRightClick(View view) {
        editHouseInfo.setNewName(mName.getRightText().toString());
        LLHttpManager.house_editinfo(this, editHouseInfo, new HttpCallback<HttpData<Void>>(this){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                editHouseInfo.reName();
                //保存修改的默认房屋
                LLHouseDBManager.getInstance(getContext()).put(editHouseInfo);
                AppApplication.getInstance().setCurHouseInfo(editHouseInfo);
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    private String getFirstShowData(){
        if(firstShowMap.containsKey(AppApplication.getInstance().getCurrentHouseSerialNo()) && firstShowMap.get(AppApplication.getInstance().getCurrentHouseSerialNo()).equals("1")){
            return AppApplication.getInstance().getString(R.string.device);
        }else{
            return AppApplication.getInstance().getString(R.string.scene);
        }
    }

    private void showBottomEditDialog(String text) {
        if (mBottomEditDialog == null) {
            mBottomEditDialog = new BottomEditDialog(this);
            mBottomEditDialog.setDialogClickListener(new BottomEditDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {
                    mName.setRightText(mBottomEditDialog.getText());
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomEditDialog);
        }
        mBottomEditDialog.setLabel(getString(R.string.house_home));
        mBottomEditDialog.setText(text);
        mBottomEditDialog.setMaxLength(Constants.HOUSE_NAME_MAX_LENGTH);
        mBottomEditDialog.show();
    }

    private void showBottomListDialog(){
        if (mBottomListDialog == null) {
            mBottomListDialog = new BottomListDialog(this);
            mBottomListDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add(getString(R.string.scene));
            data.add(getString(R.string.device));
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    firstShowMap.put(AppApplication.getInstance().getCurrentHouseSerialNo(),""+position);
                    SPUtils.getInstance().put("firstShow",JSON.toJSONString(firstShowMap));
                    mRoomShow.setRightText(getFirstShowData());
                    mBottomListDialog.dismiss();
                }
            });
            mBottomListDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomListDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomListDialog.getAdapter();
        listLabelAdapter.setSelected(getFirstShowData());
        mBottomListDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (requestCode == LocationActivity.LOCATION_REQUEST_CODE){
                LocationData locationData = (LocationData) data.getSerializableExtra("DATA");
                editHouseInfo.setProvince(locationData.province);
                editHouseInfo.setProvinceId(locationData.provinceId);
                editHouseInfo.setCity(locationData.city);
                editHouseInfo.setCityId(locationData.provinceId + locationData.cityId);
                editHouseInfo.setDistrict(locationData.district);
                editHouseInfo.setDistrictId(locationData.provinceId + locationData.cityId + locationData.districtId);
                editHouseInfo.setAddress(locationData.locationCN);
                editHouseInfo.setLongitude(new BigDecimal(locationData.longitude).setScale(6, BigDecimal.ROUND_DOWN).doubleValue());
                editHouseInfo.setLatitude(new BigDecimal(locationData.latitude).setScale(6, BigDecimal.ROUND_DOWN).doubleValue());
                editHouseInfo.setRange(50);
                mLocation.setRightText(editHouseInfo.getAddress());
            }
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        initData();
    }
}